package top.blackr.folivora.common.security.crud.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import top.blackr.folivora.common.core.R;
import top.blackr.folivora.common.log.annotation.SysLog;
import top.blackr.folivora.common.security.crud.model.entity.SysDept;
import top.blackr.folivora.common.security.crud.repository.SysDeptRepository;

import java.util.List;

@Tag(name = "部门")
@RestController
@AllArgsConstructor
@RequestMapping("dept")
public class SysDeptController {

    private final SysDeptRepository deptService;

    @GetMapping("page")
    @SaCheckPermission("dept_read")

    @Operation(summary = "树状信息", description = "树状信息")
    public R<List<Tree<Integer>>> getDeptPage() {
        return R.ok(deptService.selectTree());
    }

    @SysLog("'新增部门 ' + #params.name")
    @PostMapping("save")
    @Operation(summary = "新增部门")
    @SaCheckPermission("dept_write")

    public R<Boolean> saveDept(@RequestBody SysDept params) {
        return R.ok(deptService.saveDept(params));
    }

    @SysLog("'更新部门 ' + #params.deptId")
    @PostMapping("update")
    @Operation(summary = "更新部门")
    @SaCheckPermission("dept_write")

    public R<Boolean> updateDept(@RequestBody SysDept params) {
        return R.ok(deptService.updateDeptById(params));
    }

    @SysLog("'删除部门 ' + #params.deptId")
    @PostMapping("delete")
    @Operation(summary = "删除部门")
    @SaCheckPermission("dept_del")
    public R<Boolean> delDept(@RequestBody SysDept params) {
        return R.ok(deptService.removeDeptById(params.getDeptId()));
    }

    /**
     * 根据登录用户的部门返回部门信息
     *
     * @return 部门列表
     */
    @GetMapping("deptAuthority")
    @Operation(summary = "根据登录用户的部门返回部门信息")
    public R<?> deptAuthority() {
        return R.ok(deptService.deptAuthority());
    }

}
